<template>
  <div>
    <h1>{{id? '编辑':'新建'}}分类</h1>
    <el-form @submit.native.prevent="save">
      <el-form-item label="名称:">
        <el-input v-model="model.name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" native-type="submit">{{id? '保存':'创建'}}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    id: {},
  },
  data() {
    return {
      model: {},
      list: [],
    };
  },
  methods: {
    async save() {
      let res;
      if (this.id) {
        res = await this.$http.put(`/tags/${this.id}`, this.model);
      } else {
        res = await this.$http.post("/tags", this.model);
      }
      if (this.id) {
        this.$router.push("/tags/list");
        this.$message({
          message: "保存分类成功",
          type: "success",
        });
      } else {
        this.$router.push("/tags/list");
        this.$message({
          message: "创建分类成功",
          type: "success",
        });
      }
    },
    async fetch() {
      const res = await this.$http.get(`/tags/${this.id}`);
      this.model = res.data;
    },
    async getArticleList() {
      const res = await this.$http.get("articles");
      this.list = res.data;
      console.log(this.list);
    },
  },
  created() {
    this.id && this.fetch();
    this.getArticleList();
  },
};
</script>
<style scoped>
</style>

